package com.zlh.hdfsfilesys.controller;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ReadFile {

    public static JSONArray objects;


    /**
     * 将上传的文件以字节流读取，按行读取后转换为json格式的数据
     * @param in
     * @param title
     * @return 返回格式化的json数组
     * @throws IOException
     */
    public static JSONArray readFile( BufferedReader in,String title) throws IOException {
        objects=new JSONArray();
        String line = null;
        while ((line = in.readLine()) != null) {
            JSONObject object = new JSONObject();
            object.put("context",line);
            object.put("title",title);
            objects.put(object);
        }
        in.close();
        return objects;
    }
}
